﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Sharp.DataTransferObject;
using Sharp.Platform;
using Sharp.Platform.Configuration;
using Sharp.Platform.Owin;
using Sharp.WebBooter.Areas.CMS.Models;

namespace Sharp.WebBooter.Areas.CMS.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new IndexViewModel();

            model.NavData = SharpUserManager.Current.Permissions.Where(x => x.SystemCode == "CMS");
            model.Subscribers= GetSubscribers();
            return View(model);
        }
        /// <summary>
        /// 获取有权限而且配置了需要现在的订阅项。
        /// </summary>
        /// <returns></returns>
        private List<PermissionDTO> GetSubscribers()
        {
            var userId = SharpUserManager.Current.Id;
            var config = UserConfigManage.Read<UserConfig>(userId);
            var permissions = SharpUserManager.Current.Permissions.Where(x => x.SystemCode == "CMS");
            if (config.Subscribers.Any())
            {
                var list = new List<PermissionDTO>();
                
                config.Subscribers.OrderBy(x => x.Order).ToList().ForEach(item =>
                {
                    var info = permissions.FirstOrDefault(x => x.Id == item.Id);
                    if (info != null)
                    {
                        list.Add(info);
                    }
                });
                return list;
            }
            else
            {
                return permissions.Where(x => x.Category == "09").ToList();
            }
        }

        public ActionResult Main()
        {
            return View();
        }
    }
}